<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <el-dialog title="PShell单元卡属性修改" v-model="props.visible" @close="handleVisible('cancel')">
    <div class="query">
      <el-form ref="editPshellForm" :model="query" size="small" label-width="120" :rules="rules">
        <el-form-item label="PShell ID：">
          <span>{{ props.pshellItem.place2 }}</span>
        </el-form-item>
        <el-form-item label="材料ID：" prop="place3">
          <el-input
            readonly
            v-model="query.place3"
            placeholder="请选择材料"
            class="input-with-select"
          >
            <template #append>
              <el-link size="small" type="primary" @click="chooseMaterial()">请选择</el-link>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="材料厚度：" prop="place4">
          <el-input v-model="query.place4" placeholder="请输入材料厚度" maxlength="8"></el-input>
        </el-form-item>
        <el-form-item label="保存后的版本号：" prop="versionNumber">
          <el-input v-model="query.versionNumber" placeholder="请输入版本号"> </el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="remarks">
          <el-input v-model="query.remarks" type="textarea" :rows="3" placeholder="请输入备注信息"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" @click="handleVisible('cancel')">取 消</el-button>
        <el-button size="small" type="primary" @click="handleVisible('submit')">保存并升版</el-button>
      </div>
    </template>
  </el-dialog>
  <el-dialog title="选择材料" v-model="tableVisible" width="50%" @close="tableVisible = false"> 
    <el-table :data="materialList" style="width: 100%" highlight-current-row @current-change="handleCurrentChange">
      <el-table-column type="radio" width="55" />
      <el-table-column prop="materialName" label="材料名称" width="120" />
      <el-table-column prop="materialNumber" label="材料卡编号" width="120" />
      <el-table-column prop="e" label="E(MPa)" />    
      <el-table-column prop="nu" label="NU" />   
      <el-table-column prop="g" label="G(MPa)" />   
      <el-table-column prop="rho" label="RHO" />   
      <el-table-column prop="thmCooe" label="Thm Cooe" />   
    </el-table>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="small" type="primary" @click="tableVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="handleMaterial('submit')">确 定</el-button>
      </div>  
    </template>
  </el-dialog>
</template>
<!-- 
const row = document.getElementById('targetRow');
            row.scrollIntoView({ behavior: 'smooth' });
-->
<script name="ImportPart" setup>
import { getStructureTreeList, importPart, modifyModelManagementPshell } from '@/api/elementsLibrary';
import { getMetalMaterialList } from '@/api/lyingWarehouse';
const props = defineProps(['visible', 'pshellItem', 'partId']);
const emits = defineEmits(['handleEvent']);
const query = ref({
  place3: '',     // 材料ID                            
  place4: '',       // 材料厚度                          
  versionNumber: '',         // 保存后的版本号                                                      
  remarks: '',          // 备注信息                                                               
});   
const editPshellForm = ref(null);
const selectList = ref([]);
const modelNumberList = ref([]);
const sectionList = ref([]);
const componentList = ref([]);
const fileList = ref([]);
const materialList = ref([]);
const submitFile = ref(null);
const tableVisible = ref(false);
const selectRow = ref(null);
const rules = reactive({
  place3: [
    { required: true, message: '请选择材料', trigger: 'blur' }
  ],
  place4: [    
    { required: true, message: '请输入材料厚度', trigger: 'blur' },     
    { pattern: /^(?:[+]?\d+(?:\.\d*)?(?:[eE][-+]?\d+)?|[+]?\.\d+(?:[eE][-+]?\d+)?)$/, message: '请输入数值或科学计数法表示的正数（不多于8位）', trigger: 'blur' }
  ],                    
  versionNumber: [
    { required: true, message: '请输入版本号', trigger: 'blur' },
    { pattern: /^[a-zA-Z][a-zA-Z0-9]*$/, message: '请输入字母开头的版本号', trigger: 'blur' }
  ],
  remarks: [            
    { required: true, message: '请输入备注信息', trigger: 'blur' },         
  ],
})
const handleCurrentChange = (row) => {                    
 console.log('choosed row', row);
 selectRow.value = row;
}                       
const chooseMaterial = () => {
  tableVisible.value = true;
}
const handleMaterial = () => {
  if (selectRow.value) {
    query.value.place3 = selectRow.value.materialNumber;
    tableVisible.value = false;
  }
}
const getStructureList = () => {
  getStructureTreeList({pageSize:9999}).then((res) => {
    if (res) {
      selectList.value = res.rows;
      modelNumberList.value = res?.rows?.filter((el) => {
        return el.parentId === 0;
      });
    }
  });
};
const handleRemove = (file) => {
  submitFile.value = null;
  fileList.value = [];
};
const handleChange = (file,list) => {
  fileList.value = [];
  fileList.value.push(file);
  const fileType = file.name.split('.');
  const length = fileType.length;
  console.log(fileList.value);
  console.log(list)
  if (!(fileType[length-1] === 'bdf')) {
    ElMessage({ showClose: true, message: '文件类型错误,请上传bdf文件', type: 'error' });
    return;
  }
  submitFile.value = file.raw;
};
const handleVisible = (type) => {
  if (type === 'cancel') {
    emits('handleEvent', { type: 'cancel' });
  } else {
    //提交数据
    editPshellForm.value.validate((valid, fields) => {
      if (valid) {
        let params = {
          ...query.value,
          id: props.pshellItem?.id,
          partId: props.partId,
          cardType: props.pshellItem?.cardType,
        }
        modifyModelManagementPshell(params).then((res) => {
          if (res) {
            ElMessage({ showClose: true, message: '编辑成功！', type: 'success' });
            emits('handleEvent', { type: 'refresh' });
          }
        });
      } else {
        return false;
      }
    })
  }
};
const getMaterialList = () => { 
  getMetalMaterialList({pageSize:9999, pageNum:1}).then((res) => {
    if (res) {
      materialList.value = res.rows;
    }
  });
}

onMounted(() => {
  query.value.place3 = props.pshellItem?.place3;
  query.value.place4 = props.pshellItem?.place4;
  query.value.versionNumber = props.pshellItem?.versionNumber;
  query.value.remarks = props.pshellItem?.remarks;
  getMaterialList();
  getStructureList();
});
</script>
  <style lang="scss"coped>
</style>
  